package com.secsbrain.cloudconsole.biz.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.secsbrain.cloudconsole.biz.constant.EnumDefinition;
import com.secsbrain.cloudconsole.biz.constant.RoleCodeConstants;
import com.secsbrain.cloudconsole.biz.utils.WorkSheetUtils;
import com.secsbrain.cloudconsole.remote.SsoRemoteService;
import com.secsbrain.common.ResponseResult;
import com.secsbrain.frame.sequence.zk.ZkDistributedIdGenerater;
import com.secsbrain.sso.core.SsoHelper;
import com.secsbrain.sso.model.dto.system.RoleDTO;
import com.secsbrain.sso.model.dto.system.UserDetailDTO;
import org.apache.commons.lang3.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import com.secsbrain.cloudconsole.dal.mapper.WorkSheetMapper;
import com.secsbrain.cloudconsole.dal.model.WorkBootGroupAccess;
import com.secsbrain.cloudconsole.dal.model.WorkDeployAccess;
import com.secsbrain.cloudconsole.dal.model.WorkDeployOther;
import com.secsbrain.cloudconsole.dal.model.WorkOpsCommon;
import com.secsbrain.cloudconsole.dal.model.WorkServerApply;
import com.secsbrain.cloudconsole.dal.model.WorkSheet;
import com.secsbrain.cloudconsole.dal.model.WorkSheetOps;
import com.secsbrain.cloudconsole.remote.CmdbRemoteService;
import com.secsbrain.cmdb.client.iface.AppInfoService;
import com.secsbrain.cmdb.client.model.AppDetail;
import com.secsbrain.cmdb.client.model.AppTypeDto;
import com.secsbrain.common.ResponseVo;
import com.secsbrain.common.concurrent.ExecutorManager;
import com.secsbrain.engine.client.iface.WorkSheetFlowService;
import com.secsbrain.engine.client.model.ProcessDeal;
import com.secsbrain.engine.client.model.ProcessDto;
import com.secsbrain.engine.client.model.StartProcessParam;

import com.secsbrain.cloudconsole.biz.IWorkBootGroupAccessService;
import com.secsbrain.cloudconsole.biz.IWorkCommentService;
import com.secsbrain.cloudconsole.biz.IWorkDeployAccessService;
import com.secsbrain.cloudconsole.biz.IWorkDeployOtherService;
import com.secsbrain.cloudconsole.biz.IWorkOpsCommonService;
import com.secsbrain.cloudconsole.biz.IWorkServerApplyService;
import com.secsbrain.cloudconsole.biz.IWorkSheetOpsService;
import com.secsbrain.cloudconsole.biz.IWorkSheetService;
import com.baomidou.framework.service.impl.SuperServiceImpl;
import com.baomidou.mybatisplus.plugins.Page;

/**
 *
 * WorkSheet 表数据服务层接口实现类
 *
 */
@Service
public class WorkSheetServiceImpl extends SuperServiceImpl<WorkSheetMapper, WorkSheet> implements IWorkSheetService {
	
	private static final Logger logger = LoggerFactory.getLogger(WorkSheetServiceImpl.class);
	
	@Autowired
	private ZkDistributedIdGenerater distributedIdGenerater;
	
	@Autowired
	private WorkSheetFlowService workSheetFlowService;
	
	@Autowired
    private AppInfoService appInfoService;
	
    @Autowired
    private CmdbRemoteService cmdbRemoteService;
	  
    @Autowired
    private IWorkCommentService commentService;
	
	@Autowired
	private SsoRemoteService userServiceFacade;
	
	@Autowired
	private IWorkSheetOpsService workSheetOpsService;
	
	@Autowired
	private IWorkServerApplyService serverApplyService;
	
	@Autowired
	private IWorkOpsCommonService opsCommonService;
	
	@Autowired
	private IWorkDeployAccessService deployAccessService;
	
	@Autowired
	private IWorkDeployOtherService deployOtherService;
	
	@Autowired
	private IWorkBootGroupAccessService bootGroupAccessService;
	
	/* (non-Javadoc)
	 * @see IWorkSheetService#queryWorkSheet(WorkSheet, java.lang.String, java.lang.String, java.lang.Integer, java.lang.Integer)
	 */
	@Override
	public Page<WorkSheet> queryWorkSheet(WorkSheet workSheet, String appCode, String roleCode, Integer page, Integer limit) {
		Page<WorkSheet> pageBean=new Page<WorkSheet>(page, limit);
		
		UserDetailDTO userDetailDTO= SsoHelper.currentUser();
		List<RoleDTO> roleList=userDetailDTO.getUserRoleList();
		List<String> roleCodeList=new ArrayList<String>();
		for(RoleDTO roleDTO:roleList){
			if(appCode.equals(roleDTO.getAppCode())){
				roleCodeList.add(roleDTO.getCode());
			}
		}
		
		String roleCodeFinal=roleCodeList.get(0);
		for(String roleCodeTmp:roleCodeList){
			if(roleCodeTmp.equals(roleCode)){
				roleCodeFinal=roleCode;
				break;
			}
		}
		
		if(RoleCodeConstants.OPS.equals(roleCodeFinal)){
			workSheet.setOpsId(userDetailDTO.getId());
			List<Integer> excludeStatusList=new ArrayList<Integer>();
			if(workSheet.getStatusArray()!=null&&workSheet.getStatusArray().length>0){
				for(Integer status : workSheet.getStatusArray()){
					if(EnumDefinition.WorkSheetStatus.CREATE.getCode().equals(status)){
						excludeStatusList.add(status);
					}
				}
			}
			workSheet.setExcludeStatusArray((Integer[])excludeStatusList.toArray(new Integer[excludeStatusList.size()]));
			pageBean.setRecords(this.baseMapper.queryByOps(pageBean, workSheet));
		}else if(RoleCodeConstants.ADMIN.equals(roleCodeFinal)){
			pageBean.setRecords(this.baseMapper.queryByDemander(pageBean, workSheet));
		}else{
			workSheet.setCreatorId(userDetailDTO.getId());
			pageBean.setRecords(this.baseMapper.queryByDemander(pageBean, workSheet));
		}
		
		return pageBean;
	}
	
	/* (non-Javadoc)
	 * @see IWorkSheetService#queryWorkSheetDetail(java.lang.Long)
	 */
	@Override
	public WorkSheet queryWorkSheetDetail(Long workSheetId) {
		WorkSheet workSheet=this.selectById(workSheetId);
		Assert.notNull(workSheet, "没有找到相应的工单");
		
		UserDetailDTO userDetailDTO=SsoHelper.currentUser();
		Assert.isTrue(userDetailDTO.getId().equals(workSheet.getCreatorId())
				|| WorkSheetUtils.hasIncludeRole(userDetailDTO.getUserRoleList(), RoleCodeConstants.OPS)
				||WorkSheetUtils.hasIncludeRole(userDetailDTO.getUserRoleList(), RoleCodeConstants.ADMIN), "无权限查看此工单");
		
		Integer type=workSheet.getItype();
		if(EnumDefinition.WorkSheetType.SERVER.getCode().equals(type)){
			if(EnumDefinition.ServerSubType.APPLY.getCode().equals(workSheet.getSubType())){
				WorkServerApply workServerApply=new WorkServerApply();
				workServerApply.setSheetId(workSheet.getId());
				workSheet.setWorkServerApply(serverApplyService.selectOne(workServerApply));
			}else{
				WorkOpsCommon workOpsCommon=new WorkOpsCommon();
				workOpsCommon.setSheetId(workSheet.getId());
				workSheet.setWorkOpsCommon(opsCommonService.selectOne(workOpsCommon));
			}
		}else if(EnumDefinition.WorkSheetType.DEPLOY.getCode().equals(type)){
			if(EnumDefinition.DeploySubType.APPNEW.getCode().equals(workSheet.getSubType())){
				WorkDeployAccess workDeployAccess=new WorkDeployAccess();
				workDeployAccess.setSheetId(workSheet.getId());
				WorkDeployAccess result=deployAccessService.selectOne(workDeployAccess);
				workSheet.setWorkDeployAccess(result);
			}else if(EnumDefinition.DeploySubType.CLOUDGROUP.getCode().equals(workSheet.getSubType())){
				WorkBootGroupAccess workBootGroupAccess=new WorkBootGroupAccess();
				workBootGroupAccess.setSheetId(workSheet.getId());
				workSheet.setWorkBootGroupAccess(bootGroupAccessService.selectOne(workBootGroupAccess));
			}else{
				WorkDeployOther workDeployOther=new WorkDeployOther();
				workDeployOther.setSheetId(workSheet.getId());
				workSheet.setWorkDeployOther(deployOtherService.selectOne(workDeployOther));
			}
		}else if(EnumDefinition.WorkSheetType.DOMAIN.getCode().equals(type)){
		}else if(EnumDefinition.WorkSheetType.CONFIG.getCode().equals(type)||EnumDefinition.WorkSheetType.INSTALL.getCode().equals(type)){
			WorkOpsCommon workOpsCommon=new WorkOpsCommon();
			workOpsCommon.setSheetId(workSheet.getId());
			workSheet.setWorkOpsCommon(opsCommonService.selectOne(workOpsCommon));
		}
		return workSheet;
	}
	
	/**
	 * 功能描述: <br>
	 * 获取应用分类名
	 *
	 * @param typeId
	 * @return
	 */
	private String getTypeName(Long typeId){
		ResponseVo<List<AppTypeDto>> responseVo=appInfoService.queryAppType();
		if(!responseVo.isSuccess()){
			logger.error("查询应用分类列表接口失败："+responseVo.getMsg());
			throw new RuntimeException(responseVo.getMsg());
		}
		List<AppTypeDto> list=responseVo.getData();
		if(CollectionUtils.isEmpty(list)){
			return "";
		}
		for(AppTypeDto appTypeDto:list){
			if(appTypeDto.getId().equals(typeId)){
				return appTypeDto.getName();
			}
		}
		return "";
	}
	
	/**
	 * 功能描述: <br>
	 * 获取用户姓名Map
	 *
	 * @param idSet
	 * @return
	 */
	private Map<Long, String> getUserNameMap(Set<Long> idSet){
		Map<Long, String> map=new HashMap<Long, String>();
		if(idSet==null||idSet.size()==0){
			return map;
		}
		Long[] userIdArry = idSet.toArray(new Long[idSet.size()]);
		ResponseResult<List<UserDetailDTO>> result=userServiceFacade.getUserDTOByIds(userIdArry);
		if(result.getCode()!=1){
			logger.error("请求批量获取用户详情接口失败："+result.getMsg());
			throw new RuntimeException(result.getMsg());
		}
		List<UserDetailDTO> list=result.getData();
		if(CollectionUtils.isEmpty(list)){
			return map;
		}
		for(UserDetailDTO userDetailDTO:list){
			map.put(userDetailDTO.getId(), userDetailDTO.getNickname());
		}
		return map;
	}
	
	private String getAppName(Long appId){
		ResponseVo<AppDetail> responseVo=cmdbRemoteService.queryAppDetailById(appId);
		if(!responseVo.isSuccess()){
			logger.error("查询应用分类列表接口失败："+responseVo.getMsg());
			throw new RuntimeException(responseVo.getMsg());
		}
		AppDetail appDetail=responseVo.getData();
		return appDetail.getName();
	}
	
	private Date getDate(String date){
		try {
			return DateUtils.parseDate(date, "yyyy-MM-dd");
		} catch (Exception e) {
			logger.error("",e);
		}
		return null;
	}
	
	/* (non-Javadoc)
	 * @see IWorkSheetService#createWorkSheet(WorkSheet)
	 */
	@Override
	@Transactional
	public void createWorkSheet(WorkSheet workSheet,String appCode) {
		UserDetailDTO userDetailDTO=SsoHelper.currentUser();
//		Assert.isTrue(WorkSheetUtils.hasIncludeRole(userDetailDTO.getUserRoleList(), RoleCodeConstants.DEVELOPER), "没有提交工单的权限");
		
		StartProcessParam startProcessParam=new StartProcessParam();
    	startProcessParam.setStartUserId(userDetailDTO.getId()+"");
    	ResponseVo<ProcessDto> processVo=workSheetFlowService.create(startProcessParam);
    	if(!processVo.isSuccess()||processVo.getData()==null){
    		logger.error("创建工单流程失败："+processVo.getMsg());
    		throw new RuntimeException("创建工单失败");
    	}
    	
		Integer type=workSheet.getItype();
    	workSheet.setCode(this.generateWorkSheetCode(type));
    	workSheet.setCreateTime(new Date());
    	workSheet.setCreatorId(userDetailDTO.getId());
    	workSheet.setCreatorName(userDetailDTO.getNickname());
    	workSheet.setIstatus(EnumDefinition.WorkSheetStatus.CREATE.getCode());
    	workSheet.setProcessInstanceId(processVo.getData().getProcessInstanceId());
    	
		this.insertSelective(workSheet);
		
		if(EnumDefinition.WorkSheetType.SERVER.getCode().equals(type)){
			if(EnumDefinition.ServerSubType.APPLY.getCode().equals(workSheet.getSubType())){
				WorkServerApply workServerApply=workSheet.getWorkServerApply();
				workServerApply.setSheetId(workSheet.getId());
				workServerApply.setDueDate(this.getDate(workServerApply.getDueDateStr()));
				serverApplyService.insertSelective(workServerApply);
			}else{
				WorkOpsCommon workOpsCommon=workSheet.getWorkOpsCommon();
				workOpsCommon.setSheetId(workSheet.getId());
				opsCommonService.insertSelective(workOpsCommon);
			}
		}else if(EnumDefinition.WorkSheetType.DEPLOY.getCode().equals(type)){
			if(EnumDefinition.DeploySubType.APPNEW.getCode().equals(workSheet.getSubType())){
				WorkDeployAccess workDeployAccess=workSheet.getWorkDeployAccess();
				workDeployAccess.setSheetId(workSheet.getId());
				
				Set<Long> userIdSet=new HashSet<Long>();
				userIdSet.add(workDeployAccess.getOwnerId());
				userIdSet.add(workDeployAccess.getQualityId());
				Map<Long, String> userNameMap=this.getUserNameMap(userIdSet);
				
				workDeployAccess.setQualityName(userNameMap.get(workDeployAccess.getQualityId()));//质量负责人姓名
				workDeployAccess.setOwnerName(userNameMap.get(workDeployAccess.getOwnerId()));//开发负责人姓名
				workDeployAccess.setTypeName(this.getTypeName(workDeployAccess.getBtypeId()));//分类名称
				deployAccessService.insertSelective(workDeployAccess);
			}else if(EnumDefinition.DeploySubType.CLOUDGROUP.getCode().equals(workSheet.getSubType())){
				WorkBootGroupAccess workBootGroupAccess=workSheet.getWorkBootGroupAccess();
				workBootGroupAccess.setSheetId(workSheet.getId());
				workBootGroupAccess.setAppName(this.getAppName(workBootGroupAccess.getAppId()));
				bootGroupAccessService.insertSelective(workBootGroupAccess);
			}else{
				WorkDeployOther workDeployOther=workSheet.getWorkDeployOther();
				workDeployOther.setSheetId(workSheet.getId());
				workDeployOther.setAppName(this.getAppName(workDeployOther.getAppId()));
				deployOtherService.insertSelective(workDeployOther);
			}
		}else if(EnumDefinition.WorkSheetType.DOMAIN.getCode().equals(type)){
		}else if(EnumDefinition.WorkSheetType.CONFIG.getCode().equals(type)||EnumDefinition.WorkSheetType.INSTALL.getCode().equals(type)){
			WorkOpsCommon workOpsCommon=workSheet.getWorkOpsCommon();
			workOpsCommon.setSheetId(workSheet.getId());
			opsCommonService.insertSelective(workOpsCommon);
		}
		
		try {
			ExecutorManager.getInstance().execute(new Runnable() {
				
				@Override
				public void run() {
					commentService.sendWorkSheetDynamic(workSheet,userDetailDTO,EnumDefinition.WorkSheetStatus.CREATE.getCode(),null,appCode);
				}
			});
		} catch (Exception e) {
			logger.error("",e);
		}
		
	}
	
	/**
	 * 功能描述: <br>
	 * 生成工单编号
	 *
	 * @param type
	 * @return
	 */
		private String generateWorkSheetCode(Integer type){
		if(EnumDefinition.WorkSheetType.SERVER.getCode().equals(type)){
			String sque=distributedIdGenerater.generateId(EnumDefinition.WorkSheetType.SERVER.getPrefix());
			return EnumDefinition.WorkSheetType.SERVER.getPrefix()+sque.substring(5, sque.length());
		}else if(EnumDefinition.WorkSheetType.DEPLOY.getCode().equals(type)){
			String sque=distributedIdGenerater.generateId(EnumDefinition.WorkSheetType.DEPLOY.getPrefix());
			return EnumDefinition.WorkSheetType.DEPLOY.getPrefix()+sque.substring(5, sque.length());
		}else if(EnumDefinition.WorkSheetType.DOMAIN.getCode().equals(type)){
			String sque=distributedIdGenerater.generateId(EnumDefinition.WorkSheetType.DOMAIN.getPrefix());
			return EnumDefinition.WorkSheetType.DOMAIN.getPrefix()+sque.substring(5, sque.length());
		}else if(EnumDefinition.WorkSheetType.CONFIG.getCode().equals(type)){
			String sque=distributedIdGenerater.generateId(EnumDefinition.WorkSheetType.CONFIG.getPrefix());
			return EnumDefinition.WorkSheetType.CONFIG.getPrefix()+sque.substring(5, sque.length());
		}else if(EnumDefinition.WorkSheetType.INSTALL.getCode().equals(type)){
			String sque=distributedIdGenerater.generateId(EnumDefinition.WorkSheetType.INSTALL.getPrefix());
			return EnumDefinition.WorkSheetType.INSTALL.getPrefix()+sque.substring(5, sque.length());
		}else{
			String sque=distributedIdGenerater.generateId(EnumDefinition.WorkSheetType.OTHER.getPrefix());
			return EnumDefinition.WorkSheetType.OTHER.getPrefix()+sque.substring(5, sque.length());
		}
	}

	/* (non-Javadoc)
	 * @see IWorkSheetService#claimWorkSheet(java.lang.Long)
	 */
	@Override
	@Transactional
	public void claimWorkSheet(Long workSheetId,String appCode) {
		UserDetailDTO userDetailDTO=SsoHelper.currentUser();
		Assert.isTrue(WorkSheetUtils.hasIncludeRole(userDetailDTO.getUserRoleList(), RoleCodeConstants.OPS), "没有认领工单的权限");
		
		WorkSheet workSheet=this.selectById(workSheetId);
		Assert.notNull(workSheet, "没有找到相应的工单");
		
		WorkSheet workSheetUp=new WorkSheet();
		workSheetUp.setId(workSheetId);
		workSheetUp.setUpdateTime(new Date());
		workSheetUp.setOpsId(userDetailDTO.getId());
		workSheetUp.setOpsName(userDetailDTO.getNickname());
		workSheetUp.setIstatus(EnumDefinition.WorkSheetStatus.DEAL.getCode());
		this.updateSelectiveById(workSheetUp);
		
		WorkSheetOps workSheetOps=new WorkSheetOps();
		workSheetOps.setSheetId(workSheetId);
		workSheetOps.setOpsId(userDetailDTO.getId());
		workSheetOps.setCreateTime(new Date());
		workSheetOpsService.insertSelective(workSheetOps);
		
		ProcessDeal processDeal=new ProcessDeal();
		processDeal.setProcessInstanceId(workSheet.getProcessInstanceId());
		processDeal.setRoleCode(RoleCodeConstants.OPS);
		processDeal.setUserId(userDetailDTO.getId()+"");
		ResponseVo<String> responseVo=workSheetFlowService.claim(processDeal);
		if(!responseVo.isSuccess()){
    		logger.error("认领工单流程失败："+responseVo.getMsg());
			throw new RuntimeException(responseVo.getMsg());
		}
		
		try {
			ExecutorManager.getInstance().execute(new Runnable() {
				
				@Override
				public void run() {
					commentService.sendWorkSheetDynamic(workSheet,userDetailDTO,EnumDefinition.WorkSheetStatus.DEAL.getCode(),null,appCode);
				}
			});
		} catch (Exception e) {
			logger.error("",e);
		}
		
	}

	/* (non-Javadoc)
	 * @see IWorkSheetService#finishWorkSheet(java.lang.Long)
	 */
	@Override
	@Transactional
	public void finishWorkSheet(Long workSheetId,String message,String appCode) {
		WorkSheet workSheet=this.selectById(workSheetId);
		Assert.notNull(workSheet, "没有找到相应的工单");
		
		UserDetailDTO userDetailDTO=SsoHelper.currentUser();
		Assert.isTrue(userDetailDTO.getId().equals(workSheet.getOpsId()), "没有结束工单的权限");
		
		WorkSheet workSheetUp=new WorkSheet();
		workSheetUp.setId(workSheetId);
		workSheetUp.setUpdateTime(new Date());
		workSheetUp.setIstatus(EnumDefinition.WorkSheetStatus.FINISH.getCode());
		this.updateSelectiveById(workSheetUp);//更新工单信息
		
		WorkSheetOps workSheetOpsWhere=new WorkSheetOps();
		workSheetOpsWhere.setOpsId(userDetailDTO.getId());
		workSheetOpsWhere.setSheetId(workSheet.getId());
		
		WorkSheetOps workSheetOpsUp=new WorkSheetOps();
		workSheetOpsUp.setUpdateTime(new Date());
		workSheetOpsService.updateSelective(workSheetOpsUp, workSheetOpsWhere);//更新完成时间
		
		commentService.addComment(message,workSheet.getId());//留言  
		
		ProcessDeal processDeal=new ProcessDeal();
		processDeal.setProcessInstanceId(workSheet.getProcessInstanceId());
		processDeal.setRoleCode(RoleCodeConstants.OPS);
		processDeal.setUserId(userDetailDTO.getId()+"");
		ResponseVo<String> responseVo=workSheetFlowService.finish(processDeal);
		if(!responseVo.isSuccess()){
    		logger.error("完成工单流程失败："+responseVo.getMsg());
			throw new RuntimeException(responseVo.getMsg());
		}
		
		try {
			ExecutorManager.getInstance().execute(new Runnable() {
				
				@Override
				public void run() {
					commentService.sendWorkSheetDynamic(workSheet,userDetailDTO,EnumDefinition.WorkSheetStatus.FINISH.getCode(),message,appCode);
				}
			});
		} catch (Exception e) {
			logger.error("",e);
		}
		
	}

	/* (non-Javadoc)
	 * @see IWorkSheetService#transferWorkSheet(java.lang.Long)
	 */
	@Override
	@Transactional
	public void transferWorkSheet(Long workSheetId, Long transferId,String appCode) {
		WorkSheet workSheet=this.selectById(workSheetId);
		Assert.notNull(workSheet, "没有找到相应的工单");
		
		UserDetailDTO userDetailDTO=SsoHelper.currentUser();
		Assert.isTrue(userDetailDTO.getId().equals(workSheet.getOpsId()), "没有转移工单的权限");
		
		Assert.isTrue(!userDetailDTO.getId().equals(transferId), "工单不能转移给自己");

		Long[] userIds={transferId};
		ResponseResult<List<UserDetailDTO>> userListVo=userServiceFacade.getRoleDTOsByUserId(appCode,userIds);
		UserDetailDTO transferUserDetail=userListVo.getData().get(0);
		Assert.isTrue(transferUserDetail!=null, "没有查询到被转移者的信息");
		
		Assert.isTrue(WorkSheetUtils.hasIncludeRole(transferUserDetail.getUserRoleList(), RoleCodeConstants.OPS), "被转移者没有权限");
		
		WorkSheet workSheetUp=new WorkSheet();
		workSheetUp.setId(workSheetId);
		workSheetUp.setUpdateTime(new Date());
		workSheetUp.setOpsId(transferUserDetail.getId());
		workSheetUp.setOpsName(transferUserDetail.getNickname());
		workSheetUp.setIstatus(EnumDefinition.WorkSheetStatus.DEAL.getCode());
		this.updateSelectiveById(workSheetUp);//更新工单信息
		
		WorkSheetOps workSheetOps=new WorkSheetOps();
		workSheetOps.setSheetId(workSheetId);
		workSheetOps.setOpsId(transferId);
		workSheetOps.setCreateTime(new Date());
		workSheetOpsService.insertSelective(workSheetOps);//新增操作人
		
		commentService.addComment("工单已转交给【"+transferUserDetail.getNickname()+"】",workSheet.getId());//留言  
		
		ProcessDeal processDeal=new ProcessDeal();
		processDeal.setProcessInstanceId(workSheet.getProcessInstanceId());
		processDeal.setRoleCode(transferId+"");
		processDeal.setUserId(SsoHelper.currentUser().getId()+"");
		ResponseVo<String> responseVo=workSheetFlowService.transfer(processDeal);
		if(!responseVo.isSuccess()){
    		logger.error("转移工单流程失败："+responseVo.getMsg());
			throw new RuntimeException(responseVo.getMsg());
		}
	}

	/* (non-Javadoc)
	 * @see IWorkSheetService#closeWorkSheet(java.lang.Long)
	 */
	@Override
	@Transactional
	public void closeWorkSheet(Long workSheetId) {
		WorkSheet workSheet=this.selectById(workSheetId);
		Assert.notNull(workSheet, "没有找到相应的工单");
		
		UserDetailDTO userDetailDTO=SsoHelper.currentUser();
		Assert.isTrue(userDetailDTO.getId().equals(workSheet.getCreatorId()), "没有关闭工单的权限");
		
		WorkSheet workSheetUp=new WorkSheet();
		workSheetUp.setId(workSheetId);
		workSheetUp.setUpdateTime(new Date());
		workSheetUp.setIstatus(EnumDefinition.WorkSheetStatus.CLOSE.getCode());
		this.updateSelectiveById(workSheetUp);
		
		ProcessDeal processDeal=new ProcessDeal();
		processDeal.setProcessInstanceId(workSheet.getProcessInstanceId());
		processDeal.setRoleCode(RoleCodeConstants.DEVELOPER);
		processDeal.setUserId(userDetailDTO.getId()+"");
		ResponseVo<String> responseVo=workSheetFlowService.confirm(processDeal);
		if(!responseVo.isSuccess()){
    		logger.error("关闭工单流程失败："+responseVo.getMsg());
			throw new RuntimeException(responseVo.getMsg());
		}
	}

}